#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include "Template.h"

#ifndef NTFS_TEMPLATE_H
#define NTFS_TEMPLATE_H

#define Template_BOOT               0
#define Template_MFT                   1
#define Template_INDEX               2
#define Template_INDEXITEM     3
#define Template_ATTRCOMMON 4
/*
#define TEMPLATE_LENGTH  512

#define isStructAttr(p,arp,type) p>=(char *)&(arp->type) && p<(char *)&(arp->type)+sizeof(arp->type)
#define TemplateReg(name)  n++;             \
            for(i=(char *)&(ap->name)-p;i<(char *)&(ap->name)-p+sizeof(ap->name);i++) {        \
                if(n!=beforeN){ nowColor=n%8; beforeN=n;}; \
                *(p+i)=nowColor;            \
            }                                                                                                   \
            templateNodeName[nowType][(char *)&(ap->name)-p]=(TEMPLATENOTE){nowColor,false,""#name""}; 

#define TemplateAdd(type)                                          \
        p = (char *) malloc(TEMPLATE_LENGTH);          \
       memset(p,0,sizeof(p));                                         \
        templateAttr[type]=p;                                               \
        n=0;  nowType=type;
//模板注释
typedef struct {
    int color;   //颜色
    bool isB;   //是否加粗    
    string name; //内容
//    string des;     //中文描述
} TEMPLATENOTE;



//属性模板 0x10="000001111000002220000",  多个字节 0表示没有颜色   其他表示字节所在位置所使用的颜色值
map<int,char*> templateAttr;
map<int,map<int,TEMPLATENOTE> > templateNodeName;
//模板注释以 行为单位的的一个缓冲  
vector<TEMPLATENOTE> templateNotes;
 
  string template_getNodes(){
        string rt;
        if(getWinsize("col")<90){            templateNotes.clear();            return  rt;        }
    
        ostringstream in;  
        foreach(templateNotes,it) {
            in << getColor(0,it->color,it->isB) << it->name<< ",";
        }
        templateNotes.clear();
        in<<getColor();
        rt=in.str();
        return rt;
    }
*/
//初始化模板
void template_init(){
    char *p;
    int i,n;
    int nowType,nowColor=0,beforeN=0;
    int tlist[]={Template_BOOT,Template_MFT,Template_INDEX,Template_INDEXITEM,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0x100};
    
    TemplateAdd(Template_MFT){
         MFT_RECORD *ap=(MFT_RECORD *)p;
         TemplateReg(mft_record_number); TemplateReg(attrs_offset); TemplateReg(bytes_in_use); TemplateReg(flags);
     }
    TemplateAdd(Template_ATTRCOMMON){
         ATTR_COMMON *ap=(ATTR_COMMON *)p;
         TemplateReg(type); TemplateReg(length); TemplateReg(non_resident); TemplateReg(name_offset); TemplateReg(name_length);
    }    
    TemplateAdd(0x10){
         STANDARD_INFORMATION *ap=(STANDARD_INFORMATION *)p;
        TemplateReg(creation_time); TemplateReg(last_data_change_time); TemplateReg(last_mft_change_time); TemplateReg(last_access_time);
    }
   TemplateAdd(0x30){
        FILE_NAME_ATTR *ap=(FILE_NAME_ATTR *)p;
       TemplateReg(parent_directory); TemplateReg(last_data_change_time); TemplateReg(last_access_time); TemplateReg(data_size); TemplateReg(file_name_length); TemplateReg(file_name);TemplateReg(value_length); 
   }
   TemplateAdd(0x80){
        FILE_DATA_ATTR *ap=(FILE_DATA_ATTR *)p;
       TemplateReg(run); TemplateReg(mapping_pairs_offset);  TemplateReg(data_size);
   }
     TemplateAdd(0x90){
            IndexHeader *ap=(IndexHeader *)p;             
           TemplateReg(attr_name); TemplateReg(type); TemplateReg(firstIdxOffset); TemplateReg(flag); TemplateReg(indxAllocSize);
     }
     TemplateAdd(0xa0){
         INDEX_BLOCK *ap=(INDEX_BLOCK *)p;                
         TemplateReg(entries_offset); TemplateReg(index_length); TemplateReg(allocated_size); TemplateReg(item0); TemplateReg(magic);
     }
     
     TemplateAdd(0x20){
 
     }
}
    
  
    /**
     * 设置颜色根据属性结构key
     */
    void template_setColorByAttr(ATTR_RECORD *ar,char *p){
        int bcolor=0,color=0;
        bool B=false,X=false,S=false;
        char *arp=(char*)ar;
        if(!arp) return;
        //属性首字符加粗
        if(p==arp) B=true;
        int type=(unsigned char) arp[0];
            switch (type){
                case 0x10:
                    bcolor=1;
                    break;
                case 0x30:
                      bcolor=2;
                    break;
                case 0x80:            
                      bcolor=3;
                    break;
                case 0x90:
                     bcolor=4;
                    break;
                case 0xa0:
                     bcolor=1;
                    break;
                case 0xc0:
                case 0xe0:
                    bcolor=5;
                    break;
                case 0x20:
                case 0xb0:
                case 0xd0:
                case 0x50:
                case 0x70:
                    bcolor=6;
                    break;
                case 0x40:
                case 0x60:
                default:
                    bcolor=0;     
                    break;
            }

           int pos=p-arp;
           if(pos<=0x10 ) type=Template_ATTRCOMMON;
           //字体颜色
           if(templateAttr[type])
           color= templateAttr[type][pos] ;
           //注释
           map<int,TEMPLATENOTE> node=templateNodeName[type];
           if(color && node.count(pos)>0 && type!=Template_ATTRCOMMON) {
               templateNotes.push_back(node[pos]);
           }
                    
           if(color && color==bcolor) B=true;
            
           if(color || bcolor) setColor(bcolor=0,color,B,X,S);
    }
    
//    template_init();
    #endif /* NTFS_H */
